using micromsg;
using MicroMsg.Common.Utils;
using MicroMsg.Network;
using MicroMsg.Protocol;
using System;
namespace MicroMsg.Scene.Video
{
	public class NetSceneDownloadVideo : NetSceneBaseEx<DownloadVideoRequest, DownloadVideoResponse, DownloadVideoRequest.Builder>
	{
		private const string TAG = "NetSceneDownloadVideo";
		private const int MM_VIDEO_NETWORK_WIFI = 1;
		private const int MM_VIDEO_NETWORK_OTHER = 2;
		public DownloadVideoContext mVideoContext;
		public void doScene(DownloadVideoContext videoContext)
		{
			if (videoContext.mStatus != 0 && videoContext.mStatus != 4)
			{
				Log.smethod_1("NetSceneDownloadVideo", "doScene status error, status = " + videoContext.mStatus);
				return;
			}
			Log.smethod_5("NetSceneDownloadVideo", "netscene startup. ");
			this.mVideoContext = videoContext;
			this.mVideoContext.mScene = this;
			this.mVideoContext.mStatus = 1;
			this.doSceneEx();
		}
		private void doSceneEx()
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.TotalLen = (uint)this.mVideoContext.mVideoInfo.nTotalDataLen;
			this.mBuilder.StartPos = (uint)this.mVideoContext.mVideoInfo.nTransDataLen;
			this.mBuilder.NetworkEnv = (NetworkDeviceWatcher.isCurrentWifi() ? 1u : 2u);
			this.mBuilder.MsgId = (uint)this.mVideoContext.mVideoInfo.nMsgSvrID;
			this.mSessionPack.mCmdID = 40;
			base.endBuilder();
			this.mVideoContext.mStatus = 2;
		}
		private void OnHttpReceivedProgress(int length)
		{
		}
		protected override void onFailed(DownloadVideoRequest request, DownloadVideoResponse response)
		{
			if (this.mVideoContext != null && this.mVideoContext.isRunning())
			{
				this.mVideoContext.mStatus = 4;
				this.doSceneFinished();
				return;
			}
			Log.smethod_1("NetSceneDownloadVideo", "scene is finished, ignored onFailed. ");
		}
		protected override void onSuccess(DownloadVideoRequest request, DownloadVideoResponse response)
		{
			if (this.mVideoContext == null || !this.mVideoContext.isRunning())
			{
				Log.smethod_1("NetSceneDownloadVideo", "scene is finished, ignored response. ");
				return;
			}
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneDownloadVideo", "download video failed, ret = " + ret);
				this.mVideoContext.mStatus = 4;
				this.doSceneFinished();
				return;
			}
			Log.smethod_4("NetSceneDownloadVideo", string.Concat(new object[]
			{
				"download response success , startpos = ",
				response.StartPos,
				", total = ",
				response.TotalLen,
				", datalen = ",
				response.Data.Buffer.Length,
				", tranLen=",
				this.mVideoContext.mVideoInfo.nTransDataLen
			}));
			uint arg_E0_0 = response.StartPos;
			if ((ulong)response.StartPos + (ulong)((long)response.Data.Buffer.Length) <= (ulong)response.TotalLen)
			{
				if ((ulong)response.StartPos == (ulong)((long)this.mVideoContext.mVideoInfo.nTransDataLen))
				{
					this.mVideoContext.mVideoInfo.nTotalDataLen = (int)response.TotalLen;
					this.mVideoContext.appendVideoData(response.Data.Buffer.ToByteArray(), response.StartPos, response.Data.Buffer.Length);
					this.mVideoContext.mVideoInfo.nTransDataLen = (int)(response.StartPos + (uint)response.Data.Buffer.Length);
					if (this.mVideoContext.isDownloadCompleted())
					{
						Log.smethod_4("NetSceneDownloadVideo", "download video complete.");
						this.mVideoContext.mStatus = 5;
						this.doSceneFinished();
						return;
					}
					this.mVideoContext.updateProgressInfo(0);
					this.mVideoContext.updateContext();
					this.doSceneEx();
					return;
				}
			}
			Log.smethod_1("NetSceneDownloadVideo", "download failed ");
			this.mVideoContext.mStatus = 4;
			this.doSceneFinished();
		}
		public void doSceneFinished()
		{
			if (this.mVideoContext != null)
			{
				this.mVideoContext.updateProgressInfo(0);
				this.mVideoContext.updateContext();
				this.mVideoContext.onFinished();
				this.mVideoContext.mScene = null;
				this.mVideoContext = null;
				Log.smethod_4("NetSceneDownloadVideo", "all  Finished. ");
			}
		}
		public void doCancel()
		{
			if (this.mVideoContext != null)
			{
				base.cancel();
				this.mVideoContext.mStatus = 4;
				this.doSceneFinished();
			}
		}
	}
}
